package com.gexin.model.shop;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.shop.base.BaseShopInfo;
import com.gexin.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * 业务名称:  t_shop_info(店铺信息表) 
 */
@SuppressWarnings("serial")
public class ShopInfo extends BaseShopInfo<ShopInfo> {
	public static final ShopInfo dao = new ShopInfo();
	public static final String shop_id="48WTA6F";

	public static final String cache_id="shop:infor:";
	/**
	 * 根据create_userid 找到所有的商铺
	 */
	public List<ShopInfo> getAllByUserId(String user_id){
		return dao.find(SqlKit.sql("shopinfo.getAllByUserId"),user_id);
	}

	public List<ShopInfo> getAll(){
		return dao.find("select * from t_shop_info");
	}

	/**
	 * 查询没有禁用的店铺信息
	 * @return
	 */
	public List<ShopInfo> getOpenShopList(){
		return dao.find("select * from t_shop_info where shop_state=1");
	}


	/**
	 * 从缓存里面读取店铺信息
	 * @param shopid  店铺ID
	 * @return
	 */
	public ShopInfo cache_getShopInfoById(String shopid){
		ShopInfo info = CacheUtils.get(cache_id+shopid);
		if(info==null){
			info =findById(shopid);
			CacheUtils.set(cache_id+shopid,info,60*5);//缓存五分钟
		}
		return info;
	}
	/**
	 * 清楚缓存
	 */
	public void cache_Clear(){
		CacheUtils.del(cache_id+getShopId());
	}

	/**
	 * 详细
	 */
	public ShopInfo getShopInfoById(String id){
		SQLUtils sql =new SQLUtils(SqlKit.sql("shopinfo.getShopInfoById"));
		sql.whereEquals("t.shop_id", id);
		return dao.findFirst(sql.toString());
	}



	/**
	 * 2016-06-30
	 * fang
	 * 总后台
	 * 修改   fang   2016-08-12
	 * 为了判断是否 绑定公众号加入的
	 */

	public Page<ShopInfo> paginate(ShopInfo shopInfo, String start_time, String end_time, int pageNumber, int   pageSize){
		SQLUtils sql =new SQLUtils(" from t_shop_info s left join t_shop_user u on s.create_userid=u.user_id  left join t_shop_type t on t.id=s.shop_type_id   ");
		//sql.append("  LEFT JOIN t_wx_account_auth a on a.shop_id=s.shop_id ");
		sql.append(" where 1=1 ");
		if(shopInfo!=null){
			if(shopInfo.getShopName()!=null){
				sql.whereLike(FIELD_SHOP_NAME, shopInfo.getShopName());
			}
		}
		//时间查询
		if (start_time != null && end_time != null ) {
			sql.append(" and s.createtime >= "+"'"+start_time+"'" +" and "+" s.createtime <= "+"'"+end_time+"'");
		}
		//sql.append(" and o.shop_id = "+"'"+shop_id+"'");
		sql.append(" order by s.createtime desc");
		return dao.paginate(pageNumber, pageSize, "select s.*,u.master_name,t.type_name as public_name ", sql.toString());
	}
	/**
	 * xu 2016-07-12
	 * 根据user_id获取其下所有店铺的详细信息
	 */
	public List<ShopInfo> getShopListByUserId(int user_id){
		SQLUtils sql=new SQLUtils("select t.* from t_shop_info t left join t_shop_user_rel s on t.shop_id=s.shop_id where s.user_id=? ");
		sql.append(" order by createtime desc ");
		return dao.find(sql.toString(),user_id);
	}
	/**
	 * xu 2016-07-14
	 * 根据shop_id获取店铺信息
	 */
	public ShopInfo getShopInfoByShopId(String shop_id){
		String sql="select * from t_shop_info where shop_id=? ";
		return dao.findFirst(sql,shop_id);
	}
	/**
	 * fang  2016-08-07
	 * 获取所有的店铺
	 *
	 */
	public List<ShopInfo> findAll(){
		SQLUtils sql=new SQLUtils("select * from t_shop_info ");
		sql.append(" order by createtime desc ");
		return dao.find(sql.toString());
	}
	/**
	 * xu 2016-08-12
	 * 查询商家用户创建的店铺数
	 */
	public ShopInfo getShopNum(int user_id){
		String sql="select count(*) as shop_num from t_shop_info where create_userid=? ";
		return dao.findFirst(sql,user_id);
	}
	/**
	 * xu 2016-08-31
	 * 所有店铺分页
	 */
	public Page<ShopInfo> findAllShop(ShopInfo shop, int pageNumber){
		SQLUtils sql=new SQLUtils(" from t_shop_info where 1=1 ");
		if (StringUtil.isNotEmpty(shop.getShopName())) {
			sql.whereLike("shop_name", shop.getShopName());
		}
		sql.append(" order by createtime desc ");
		return paginate(pageNumber, 10, "select * ", sql.toString());
	}

}
